<script setup lang="ts">
import { getHotGoodsAPI } from '@/api/detail.ts';
import { HotGoodsRequest } from '@/api/model/detailModel.ts';
import { computed, onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';

const props = defineProps<{
  type: number;
}>();

const TYPEMAP = {
  1: '24小时热销榜',
  2: '周热销榜'
};

// 使用 as keyof typeof TYPEMAP 将 props.type 断言为 TYPEMAP 中的键。
const title = computed(() => TYPEMAP[props.type as keyof typeof TYPEMAP]);

// eslint-disable-next-line no-undef
const hotList = ref([] as Good[]);

const route = useRoute();
//获取热销榜数据
const getHotList = async () => {
  //修改请求参数
  const res = await getHotGoodsAPI({
    id: route.params.id,
    type: props.type
  } as HotGoodsRequest);
  hotList.value = res.result;
};
onMounted(() => getHotList());
</script>

<template>
  <div class="goods-hot">
    <h3>{{ title }}</h3>
    <!-- 商品区块 -->
    <RouterLink to="/" class="goods-item" v-for="good in hotList" :key="good.id">
      <img :src="good.picture" alt="" />
      <p class="name ellipsis">{{ good.name }}</p>
      <p class="desc ellipsis">{{ good.desc }}</p>
      <p class="price">&yen;{{ good.price }}</p>
    </RouterLink>
  </div>
</template>

<style scoped lang="scss">
.goods-hot {
  h3 {
    height: 70px;
    background: $helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }

  .goods-item {
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }
}
</style>
